package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * The main window pannel.
 */
public class UserPanel extends JPanel 
{
	final int BUTTON_LENGTH = 150;
	final int BUTTON_WIDTH = 70;	
	
	public UserPanel()
	{	
		setLayout(new FlowLayout());
		
		try {
			Settings.load();
		} catch (IOException e) {
			/* It's ok - default values */
		} catch (ClassNotFoundException e) {
			System.out.println("Class problem in reading");
			e.printStackTrace();
		}
		addPicture();
		addStandartButtons();
		addMenuButtons();
	}

	/**
	 * 
	 */
	private void addPicture()
	{
		//D:\Technion\Jane\OS Project\GUI\
		File f = new File("fw.jpg");
		if(f.exists())
			System.out.println("HEY!!!!!!");
		
		JPanel picture = new JPanel();		
		ImageIcon image = new ImageIcon("fw.jpg");
	
		JLabel background = new JLabel(image);

		background.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());
		picture.add(background);
		picture.setVisible(true);

/*		JPanel panel = new JPanel();
		panel.setOpaque(false);
		panel.add( new JButton( "Hello" ) );
		MainFrame.getInstance().setContentPane( panel );*/

	    add(picture, BorderLayout.EAST);
	}

	/**
	 * 
	 */
	private void addStandartButtons() 
	{
		Box tableBox = Box.createHorizontalBox();
		
		JButton finish = new JButton("Finish");
		finish.setToolTipText("Close and save changes");
		JButton cancel = new JButton("Cancel");
		cancel.setToolTipText("Close without saving changes");
		
		tableBox.add(finish);
		tableBox.add(cancel);
		
		setLayout(new BorderLayout());    
		
	    
		finish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				try {
					Settings.save();
				} catch (IOException e) {
					System.out.println("Output file isn't defined");
				}

				//TODO: remember status
				System.exit(0);				
			}	  
		});
		  
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				System.exit(0);				
			}	  
		});

	    add(tableBox, BorderLayout.SOUTH);
	}

	/**
	 * 
	 */
	private void addMenuButtons() 
	{
		JPanel menu = new JPanel(new GridLayout(7, 1));

		JButton pass = new JButton("Password Manager");
		menu.add(pass);
		pass.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				//TODO: call frame of Maya	
			}	  
		});
		
		JButton rules = new JButton("Rules");
		menu.add(rules);
		rules.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				if(Settings.userMode.equals(UserMode.standartUser))
				{
					RulesStandartFrame frame = RulesStandartFrame.getInctance();
					frame.setVisible(true);
				}
				else if (Settings.userMode.equals(UserMode.advancedUser))
				{
					
				}
				else
				{
					System.out.println("Problem in user mode definition");
				}
			}	  
		});
		
		JButton logger = new JButton("Logger");
		menu.add(logger);
		logger.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				LoggerFrame frame = LoggerFrame.getInctance();
				frame.setVisible(true);
			}	  
		});		
		
		menu.add(new JButton("Exceptions List"));
		menu.add(new JButton("Help"));
		menu.add(new JButton("FW On/Off"));	

	    add(menu, BorderLayout.WEST);
	}
}
